package forum.servlet;

import forum.dao.ReplyDao;
import forum.model.Reply;
import forum.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author haomin
 * @date 2022/07/01 20:19
 **/

@WebServlet("/reply.do")
public class ReplyDoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String content = req.getParameter("content");
        String tidString = req.getParameter("type");

        int tid = Integer.parseInt(tidString);

        User currentUser = null;
        HttpSession session = req.getSession(false);
        if(session != null){
            currentUser = (User) session.getAttribute("currentUser");
        }
        if (currentUser == null) {
            // 说明没有登录
            resp.sendRedirect("/login.html");
            return;
        }

        ReplyDao replyDao = new ReplyDao();
        replyDao.insert(tid,currentUser.uID,content);

        resp.sendRedirect("/reply.html?tid=" + tid);
    }
}